﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace QLVMBAYDomain
{
    public enum DIEU_KIEN_TIM:int
    {
        
        GIA_NHO_HON_BANG=0,
        GIA_LON_HON_BANG
    }
    public class CommonDO
    {
        public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {
            DataTable dtReturn = new DataTable();

            // column names 
            PropertyInfo[] oProps = null;
            // Use reflection to get property names, to create table, Only first time, others  will follow 
            if (oProps == null)
            {
                oProps = (typeof(T)).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;

                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = typeof(object);
                    }

                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }
            foreach (T rec in varlist)
            {
                DataRow dr = dtReturn.NewRow();
                foreach (PropertyInfo pi in oProps)
                {
                    dr[pi.Name] = pi.GetValue(rec, null) == null ? null : pi.GetValue
                    (rec, null);
                }
                dtReturn.Rows.Add(dr);
            }
            dtReturn.AcceptChanges();
            return dtReturn;
        }
    }
}
